/*
 * bus.cpp
 *
 */

#include <unistd.h>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <exception>
#include "Argv.h"
#include "logger/logger.h"
#include "StringUtils.h"
#include "ProcessAux.h"
#include "API/Puerta.h"
#include "API/Bus.h"
#include "Middleware/IPCCommunicationService.h"
#include "Middleware/MiddlewareMessages.h"

int main(int argc, char **argv)
{
    Argv arguments;
    BusId busId;
    SalaId salaOrig;
    SalaId salaDest;

    arguments.setArgv(argv);

    busId = StringUtils::StringToNumber<int>(arguments.getArg(1));

    std::stringstream ss;
    ss<<"Bus "<< busId;

    salaOrig = (busId*2)-1;
    salaDest = (busId*2);

	CLogger::create(ss.str());
	CLogger::setLevel(LogLevelEnum::DEBUG);

	try{

		API::Bus bus(busId);

		ss.str("");
		ss<<"Bus started with origin:"<< salaOrig << " dest:"<<salaDest;

		CLogger::log(ss.str(),LogLevelEnum::INFO);

		bus.run(salaOrig,salaDest);

		CLogger::log("Bus ended.",LogLevelEnum::INFO);
	}
	catch (std::exception &e)
	{
		CLogger::log(e.what(),LogLevelEnum::FATAL);
	}
	return 0;
}
